package cn.itcast.sensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import cn.itcast.mobilesafe.R;

public class SensorActivity extends Activity {
	private ImageView imageView;
	private SensorManager manager;
	private SensorListener listener = new SensorListener();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_sensor);

		imageView = (ImageView) this.findViewById(R.id.imageView);
		imageView.setKeepScreenOn(true);
		manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	}

	@Override
	protected void onPause() {
		manager.unregisterListener(listener);
		super.onPause();
	}

	@Override
	protected void onResume() {
		Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		manager.registerListener(listener, sensor,
				SensorManager.SENSOR_DELAY_GAME);
		super.onResume();
	}

	private final class SensorListener implements SensorEventListener {
		private float predegree = 0;

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			float degree = event.values[0];
			RotateAnimation animation = new RotateAnimation(predegree, -degree,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation.setDuration(200);
			imageView.startAnimation(animation);
			predegree = -degree;
		}

	}

}